package lumis.service.portalmanagement.webfilereplication;

import java.util.Collection;

import javax.persistence.EntityManager;

import lumis.doui.control.DataBoundControl;
import lumis.doui.datatype.DouiDataTypeFactory;
import lumis.doui.datatype.IDouiDataType;
import lumis.doui.source.TabularSource;
import lumis.portal.PortalException;
import lumis.portal.manager.ManagerFactory;
import lumis.portal.webfilereplication.WebFileReplicationProperty;
import lumis.portal.webfilereplication.WebFileReplicationPropertyValue;
import lumis.portal.webfilereplication.WebFileReplication;
import lumis.portal.webfilereplication.WebFileReplicationType;
import lumis.util.XmlUtil;

/**
 * Control that creates the user interface for web file replication properties.
 * 
 * 
 * @version $Revision: 13092 $ $Date: 2011-05-28 18:19:06 -0300 (Sat, 28 May 2011) $
 * @since 6.0.0
 */
public class WebFileReplicationPropertiesControl extends DataBoundControl<TabularSource<?>>
{
	//string to be compared with the property name, so the right control can be created.
	private final String FIELD_TO_VALIDATE = "Target Folder";
	
	@Override
	public void setRenderData() throws PortalException
	{
		super.setRenderData();

		EntityManager entityManager = ManagerFactory.getEntityManager();

		StringBuilder subControlsXmlString = new StringBuilder(1000);
		subControlsXmlString.append(XmlUtil.XML_HEADER);

		String replicationId = this.getSource().getData().get(getSource().getPrimaryKeyField().getId(), String.class);

		DataBoundControl<?> typeControl = (DataBoundControl<?>) douiContext.getControlContainer().getControlById("type");

		String typeValue = (String) typeControl.getValue();
		WebFileReplication replication = null;

		if (replicationId != null)
		{
			replication = entityManager.find(WebFileReplication.class, replicationId);
		}

		if (typeValue != null)
		{
			WebFileReplicationType type = entityManager.getReference(WebFileReplicationType.class, typeValue);

			Collection<WebFileReplicationProperty> properties = type.getProperties();

			subControlsXmlString.append("<controls>");
			subControlsXmlString.append("<control type=\"lum_controlGroup\" title=\"STR_PROPERTIES\">");
			subControlsXmlString.append("<control type=\"lum_table\">");

			for (WebFileReplicationProperty property : properties)
			{
				subControlsXmlString.append("<tr>");
				subControlsXmlString.append("<td>");
				subControlsXmlString.append("<control type=\"lum_label\" text=\"" + property.getName() + "\" sourceId=\"none\" />");
				subControlsXmlString.append("</td>");
				subControlsXmlString.append("<td>");

				IDouiDataType dataType = DouiDataTypeFactory.getInstance(property.getDataType());
				String controlType = dataType.getDefaultInputControlType();

				subControlsXmlString.append("<control type=\"" + controlType + "\" id=\"" + property.getId() + "\" sourceId=\"none\" dataType=\"" + dataType.getId() + "\" ");
				if (replication != null && replication.getType().getId().equals(typeValue))
				{
					for (WebFileReplicationPropertyValue propertyValue : replication.getPropertyValues())
					{
						if (property == propertyValue.getProperty())
						{
							String value = propertyValue.getValue();
							if (value != null)
								subControlsXmlString.append("value=\"" + XmlUtil.encodeXml(value) + "\" ");
							break;
						}
					}
				}
				subControlsXmlString.append(">");
				//verifies the right field to insert validation.
				 if (property.getName().equals(FIELD_TO_VALIDATE))
					 subControlsXmlString.append("<control type=\"lum_regularExpressionValidator\" expression=\"^(?!.*\\.\\.).*$\"  errorMessage=\"STR_INVALID_TARGET_NAME\" value=\"STR_DEFAULT_VALIDATOR_VALUE\" />\"");
				
				subControlsXmlString.append("</control>");
				subControlsXmlString.append("</td>");
				subControlsXmlString.append("</tr>");
			}

			subControlsXmlString.append("</control>");
			subControlsXmlString.append("</control>");
			subControlsXmlString.append("</controls>");

			appendSubControls(subControlsXmlString.toString());
		}
	}
}
